﻿using B.MESUI.Login.EquipmentManagement.DTO;
using B.MESUI.Login.LoginDtos;
using DevExpress.Pdf.Native.BouncyCastle.Asn1.X509;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkflowCore2210A.Application.Dto;
using WorkflowCore2210A.Application.Dto.Input;
using WorkflowCore2210A.Application.Dto.Output;

namespace B.MESUI.Login.WorkflowCore.MyAudit
{
    public partial class Audit : DevExpress.XtraEditors.XtraForm
    {
        private string _workflowId;
        private RichTextBox rtbOpinion;
        private readonly HttpClient _httpClient = new HttpClient(); // 只需声明一次


        public Audit(string workflowId)
        {
            InitializeComponent();
            this.CenterToParent();
            _workflowId = workflowId; // 保存为成员变量
            LoadDetailAsync(workflowId);
        }



        private async void LoadDetailAsync(string workflowId)
        {
            try
            {
                // 这里请替换为你的实际接口地址
                string url = $"{BaseUrl.WorkflowUrl}Workflow/GetWorkflowDetail?workflowId={workflowId}";
                var response = await _httpClient.GetAsync(url);
                response.EnsureSuccessStatusCode();
                var json = await response.Content.ReadAsStringAsync();

                // 假设返回格式为 { code: 200, data: {...} }
                var result = JsonConvert.DeserializeObject<WorkflowCore2210A.Application.Dto.ApiPaging<WorkflowInstalceDetailOutput>>(json);
                if (result != null && result.Code == 200)
                {
                    var detail = result.Data;
                    // ... 其他字段显示

                    // 获取formData字符串
                    string formDataString = detail.FormData; // 或 detail.auditList[0].formData
                    var formDataList = JsonConvert.DeserializeObject<List<FormField>>(formDataString);

                    int startY = 20; // 控件起始Y坐标
                    int spaceY = 45; // 控件间距，适当加大
                    Font bigFont = new Font("微软雅黑", 14F, FontStyle.Regular);

                    foreach (var field in formDataList)
                    {
                        // 显示字段名
                        Label lbl = new Label();
                        lbl.Text = field.DisplayFieldName + "：";
                        lbl.Location = new Point(20, startY);
                        lbl.AutoSize = true;
                        lbl.Font = bigFont;
                        this.Controls.Add(lbl);

                        // 显示字段值
                        Label txt = new Label();
                        txt.Text = field.DisplayValue;
                        txt.Location = new Point(180, startY - 3);
                        txt.Width = 300;
                        txt.Height = 40;
                        txt.Font = bigFont;
                        this.Controls.Add(txt);

                        startY += spaceY;
                    }

                    // 生成“通过”和“驳回”按钮
                    int btnWidth = 120;
                    int btnHeight = 45;
                    int btnSpace = 30;
                    int formPadding = 40;
                    int btnY = this.ClientSize.Height - btnHeight - formPadding;
                    int btnRejectX = this.ClientSize.Width - btnWidth * 2 - btnSpace - formPadding;
                    int btnApproveX = this.ClientSize.Width - btnWidth - formPadding;

                    Button btnReject = new Button();
                    btnReject.Text = "驳回";
                    btnReject.Font = bigFont;
                    btnReject.Size = new Size(btnWidth, btnHeight);
                    btnReject.Location = new Point(btnRejectX, btnY);
                    btnReject.Click += (s, e) => { MessageBox.Show("已点击驳回"); };
                    //this.Controls.Add(btnReject);

                    Button btnApprove = new Button();
                    btnApprove.Text = "通过";
                    btnApprove.Font = bigFont;
                    btnApprove.Size = new Size(btnWidth, btnHeight);
                    btnApprove.Location = new Point(btnApproveX, btnY);
                    btnApprove.Click += async (s, e) => {
                        string opinion = rtbOpinion.Text.Trim();
                        if (string.IsNullOrEmpty(opinion))
                        {
                            MessageBox.Show("请填写审批意见！");
                            return;
                        }

                        try
                        {
                                string urls = $"{BaseUrl.WorkflowUrl}Workflow/Audit";
                                var postData = new AuditInput
                                {
                                    WorkflowId = _workflowId, // 用成员变量
                                    Remark = opinion,
                                    AuditOperation="通过",
                                    AuditUserId = AppSession.UserId,
                                };
                                var jsons = JsonConvert.SerializeObject(postData);
                                var content = new StringContent(jsons, Encoding.UTF8, "application/json");
                                var responses = await _httpClient.PostAsync(urls, content);
                                responses.EnsureSuccessStatusCode();
                                var respStr = await responses.Content.ReadAsStringAsync();

                                // 你可以根据返回内容判断是否成功
                                MessageBox.Show("审批通过已提交！");
                                DialogResult = DialogResult.OK;
                                this.Close();

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("提交失败：" + ex.Message);
                        }
                    };
                    this.Controls.Add(btnApprove);

                    // 生成富文本意见输入框
                    Label lblOpinion = new Label();
                    lblOpinion.Text = "审批意见：";
                    lblOpinion.Font = bigFont;
                    lblOpinion.AutoSize = true;

                    // 位置：按钮左侧，底部对齐
                    int opinionBoxWidth = 350;
                    int opinionBoxHeight = 60;
                    int opinionBoxX = btnRejectX - opinionBoxWidth - 30; // 按钮左侧留30像素
                    int opinionBoxY = btnY;

                    lblOpinion.Location = new Point(opinionBoxX, opinionBoxY - 35);

                    rtbOpinion = new RichTextBox();
                    rtbOpinion.Font = bigFont;
                    rtbOpinion.Location = new Point(opinionBoxX, opinionBoxY);
                    rtbOpinion.Size = new Size(opinionBoxWidth, opinionBoxHeight);

                    this.Controls.Add(lblOpinion);
                    this.Controls.Add(rtbOpinion);
                }
                else
                {
                    MessageBox.Show("获取详情失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("请求异常：" + ex.Message);
            }
        }




    }





}